package com.ryh.controller;

import java.util.Date;
import com.ryh.entity.LendingRecord;
import com.ryh.entity.Book;
import com.ryh.service.BookService;
import com.ryh.service.LendingRecordService;
import com.ryh.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private ReaderService ReaderService;

    @Autowired
    private BookService bookService;

    @Autowired
    private LendingRecordService lendingRecordService;

    @GetMapping("/profile")
    public String showReaderProfile(Model model) {
        // 获取当前登录用户信息并添加到模型中
        // 假设从会话中获取当前用户ID
        String ReaderId = "currentReaderId";
        model.addAttribute("Reader", ReaderService.getReaderById(ReaderId));

        // 获取当前用户的借阅记录并添加到模型中
        List<LendingRecord> lendingRecords = lendingRecordService.getLendingRecordsByReaderId(ReaderId);
        model.addAttribute("lendingRecords", lendingRecords);

        return "Reader/profile";
    }

    @GetMapping("/books")
    public String showBooks(Model model) {
        List<Book> books = bookService.getAllBooks();
        model.addAttribute("books", books);
        return "Reader/books";
    }

    @GetMapping("/book/{bookId}")
    public String showBookDetails(@PathVariable("bookId") String bookId, Model model) {
        Book book = bookService.getBookById(bookId);
        model.addAttribute("book", book);
        return "Reader/bookDetails";
    }

    @PostMapping("/borrow/{bookId}")
    public String borrowBook(@PathVariable("bookId") String bookId) {
        // 实现借阅图书的逻辑
        // 假设从会话中获取当前用户ID
        String ReaderId = "currentReaderId";

        // 检查图书库存是否充足
        Book book = bookService.getBookById(bookId);
        if (book.getStock() > 0) {
            // 创建借阅记录
            LendingRecord lendingRecord = new LendingRecord();
            lendingRecord.setBookId(bookId);
            lendingRecord.setReaderId(ReaderId);
            // 设置借阅日期、应还日期等
            lendingRecordService.addLendingRecord(lendingRecord);

            // 更新图书库存
            book.setStock(book.getStock() - 1);
            bookService.updateBook(book);
        }

        return "redirect:/Reader/books";
    }

    @PostMapping("/return/{recordId}")
    public String returnBook(@PathVariable("recordId") String recordId) {
        // 实现归还图书的逻辑
        LendingRecord lendingRecord = lendingRecordService.getLendingRecordById(recordId);
        if (lendingRecord != null) {
            // 更新实际归还日期
            lendingRecord.setActualReturnDate(new Date());
            lendingRecordService.updateLendingRecord(lendingRecord);

            // 更新图书库存
            Book book = bookService.getBookById(lendingRecord.getBookId());
            book.setStock(book.getStock() + 1);
            bookService.updateBook(book);
        }

        return "redirect:/Reader/profile";
    }
}